package com.ruoyi.cms.controller;

import java.util.List;

import com.ruoyi.cms.dto.DisplayLogoDTO;
import com.ruoyi.cms.dto.DisplayVideoDTO;
import com.ruoyi.cms.vo.LogoVO;
import com.ruoyi.cms.vo.VideoVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cms.domain.CDisplay;
import com.ruoyi.cms.service.ICDisplayService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 展示屏管理Controller
 * 
 * @author Choi
 * @date 2021-08-23
 */
@RestController
@RequestMapping("/cms/display")
public class CDisplayController extends BaseController
{
    @Autowired
    private ICDisplayService cDisplayService;

    /**
     * 取消关联视频
     */
    @PreAuthorize("@ss.hasPermi('cms:display:video:delete')")
    @DeleteMapping("/videos")
    public AjaxResult deleteVideos(@RequestBody @Validated DisplayVideoDTO displayVideoDTO) {
        cDisplayService.deleteDisplayVideo(displayVideoDTO);
        return AjaxResult.success();
    }

    /**
     * 添加关联视频
     */
    @PreAuthorize("@ss.hasPermi('cms:display:video:add')")
    @PostMapping("/videos")
    public AjaxResult addVideos(@RequestBody @Validated DisplayVideoDTO displayVideoDTO) {
        cDisplayService.addDisplayVideo(displayVideoDTO);
        return AjaxResult.success();
    }

    /**
     * 关联视频
     */
    @PreAuthorize("@ss.hasPermi('cms:display:video:list')")
    @GetMapping("/{id}/videos")
    public AjaxResult videos(@PathVariable long id) {
        List<VideoVO> videos = cDisplayService.getDisplayVideos(id);
        return AjaxResult.success(videos);
    }


    /**
     * 修改关联logo
     */
    @PreAuthorize("@ss.hasPermi('cms:display:logo:edit')")
    @PutMapping("/logos")
    public AjaxResult deleteLogos(@RequestBody @Validated DisplayLogoDTO displayLogoDTO) {
        cDisplayService.editDisplayLogo(displayLogoDTO);
        return AjaxResult.success();
    }

    /**
     * 取消关联logo
     */
    @PreAuthorize("@ss.hasPermi('cms:display:logo:delete')")
    @DeleteMapping("/{id}/logos")
    public AjaxResult deleteLogos(@PathVariable long id,
                                  @RequestBody @Validated long[] logoIds) {
        if (logoIds != null && logoIds.length > 0) {
            cDisplayService.deleteDisplayLogo(id, logoIds);
        }
        return AjaxResult.success();
    }

    /**
     * 关联logo
     */
    @PreAuthorize("@ss.hasPermi('cms:display:logo:add')")
    @PostMapping("/logos")
    public AjaxResult addLogos(@RequestBody @Validated List<DisplayLogoDTO> list) {
        if (list != null && list.size() > 0) {
            cDisplayService.addDisplayLogo(list);
        }
        return AjaxResult.success();
    }

    /**
     * 获取logo
     */
    @PreAuthorize("@ss.hasPermi('cms:display:logo:list')")
    @GetMapping("/{id}/logos")
    public AjaxResult logos(@PathVariable long id) {
        List<LogoVO> logos = cDisplayService.getAllLogosById(id);
        return AjaxResult.success(logos);
    }

    /**
     * 获取关联logo
     */
    @PreAuthorize("@ss.hasPermi('cms:display:logo:list')")
    @GetMapping("/{id}/logo")
    public AjaxResult logo(@PathVariable long id) {
        List<LogoVO> logos = cDisplayService.getLogosById(id);
        return AjaxResult.success(logos);
    }

    /**
     * 查询展示屏管理列表
     */
    @PreAuthorize("@ss.hasPermi('cms:display:list')")
    @GetMapping("/list")
    public TableDataInfo list(CDisplay cDisplay)
    {
        startPage();
        List<CDisplay> list = cDisplayService.selectCDisplayList(cDisplay);
        return getDataTable(list);
    }

    /**
     * 导出展示屏管理列表
     */
    @PreAuthorize("@ss.hasPermi('cms:display:export')")
    @Log(title = "展示屏管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CDisplay cDisplay)
    {
        List<CDisplay> list = cDisplayService.selectCDisplayList(cDisplay);
        ExcelUtil<CDisplay> util = new ExcelUtil<CDisplay>(CDisplay.class);
        return util.exportExcel(list, "展示屏管理数据");
    }

    /**
     * 获取展示屏管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('cms:display:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(cDisplayService.selectCDisplayById(id));
    }

    /**
     * 新增展示屏管理
     */
    @PreAuthorize("@ss.hasPermi('cms:display:add')")
    @Log(title = "展示屏管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CDisplay cDisplay)
    {
        return toAjax(cDisplayService.insertCDisplay(cDisplay));
    }

    /**
     * 修改展示屏管理
     */
    @PreAuthorize("@ss.hasPermi('cms:display:edit')")
    @Log(title = "展示屏管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CDisplay cDisplay)
    {
        return toAjax(cDisplayService.updateCDisplay(cDisplay));
    }

    /**
     * 删除展示屏管理
     */
    @PreAuthorize("@ss.hasPermi('cms:display:remove')")
    @Log(title = "展示屏管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cDisplayService.deleteCDisplayByIds(ids));
    }
}
